package impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.LevelDao;
import util.C3P0Util;

public class LevelDaoImpl implements LevelDao {

	@Override
	public List<String> queryNameList() throws SQLException {
		Connection connection = null;
		PreparedStatement ps = null;
		List<String> list = new ArrayList<String>();
		try {
			connection = C3P0Util.getConnection();
			// 不插入id 因为id为自增的
			String sql = "select name from t_level";
			ps = (PreparedStatement) connection.prepareStatement(sql);		
			ResultSet rs =  ps.executeQuery();
			while(rs.next()){
				list.add(rs.getString("name"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			C3P0Util.close(connection);
		}
		return list;
	}

	@Override
	public String queryNameById(int id) throws SQLException {
		Connection connection = null;
		PreparedStatement ps = null;
		String name = "";
		try {
			connection = C3P0Util.getConnection();
			// 不插入id 因为id为自增的
			String sql = "select name from t_level where id=?";
			ps = (PreparedStatement) connection.prepareStatement(sql);	
			ps.setInt(1, id);
			ResultSet rs =  ps.executeQuery();
			if(rs.next()){
				name = rs.getString("name");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			C3P0Util.close(connection);
		}
		return name;
	}

}
